/**************************************************************************
 *		   Copyright(c)	2006 by	Sunplus	Technology Co.,	Ltd.			  *
 *																		  *
 *	This software is copyrighted by	and	is the property	of Sunplus		  *
 *	Technology Co.,	Ltd. All rights	are	reserved by	Sunplus	Technology	  *
 *	Co., Ltd. This software	may	only be	used in	accordance with	the		  *
 *	corresponding license agreement. Any unauthorized use, duplication,	  *
 *	distribution, or disclosure	of this	software is	expressly forbidden.  *
 *																		  *
 *	This Copyright notice "M U S T"	not	be removed or modified without	  *
 *	prior written consent of Sunplus Technology	Co., Ltd.				  *
 *																		  *
 *	Sunplus	Technology Co.,	Ltd. reserves the right	to modify this		  *
 *	software without notice.											  *
 *																		  *
 *	Sunplus	Technology Co.,	Ltd.										  *
 *	19,	Innovation First Road, Science-Based Industrial	Park,			  *
 *	Hsin-Chu, Taiwan, R.O.C.											  *
 *																		  *
 *	Author:	Ted	Chiu													  *
 **************************************************************************/
#include	"general.h"
#include	"stdio.h"

#include	"panel.h"

#include	"disp_common.h"
#include	"disp_api.h"
#include	"disp_icb.h"

#define	TD024THEB2_052_27MHZ	0

#define	CCIR601_ENABLE	0
#define	UPS052_ENABLE	0

#if	SPCA==5000
void tmrUsHardWait(UINT32 delay);
#else
#include "hw_cpu.h"
#endif

extern	pSpiFun	dispSpiCtrl;
extern  pSpiCtrlFun spiRST;
void	TD024THEB2_Flip(UINT32 mode);

#define	R02_INPUT_FORMAT_RGBD		0x00
#define	R02_INPUT_FORMAT_YUV		0x09
#define	R02_INPUT_FORMAT_THROUGH	0x02
#define	R5_INIT_VALUE	0x2B

#define	MINTON_YY_DEBUG	0
#if	MINTON_YY_DEBUG
UINT32	disp_reset_time_lo = 2;	//ms
UINT32	disp_reset_time_hi = 10;
#endif

static	UINT32	TD024THEB2_InitData[] = {
	//(0x03<<10)+0x31,	/* R03[0]=1,Non-interlace */
	(0x03<<10)+0x01,	/* R03[0]=1,Non-interlace */
	//(0x05<<10)+R5_INIT_VALUE,	/* R05[0]=Horizontal reverse(0), R05[1]=Vertical reverse(0) */
	//(0x06<<10)+0x18,
	//(0x07<<10)+0x68,
	//(0x08<<10)+0x00,
	//(0x09<<10)+0x20,
	//(0x0A<<10)+0x20,
	//(0x0B<<10)+0x20,
	//(0x0C<<10)+0x10,
	//(0x10<<10)+0x35,
	//(0x11<<10)+0x38,
	//(0x14<<10)+0x98,
	//(0x15<<10)+0x9A,
	//(0x16<<10)+0xA9,
	//(0x17<<10)+0x99,
	//(0x18<<10)+0x08
	/* (0x04<<10)+0x0F */
	};
/*
CSH	1	GBR	--	ODD
CSV	0	RGB	--	EVN
*/

#if	SPCA53X
static	UINT32	color[4] = {
		0x06, 0x0F, 0x0A, 0x0D	/* TD024THEB2_051 */
	};
#endif

void TD024THEB2_Vd(UINT32 count);
void TD024THEB2_VdSet(UINT32 hi);

UINT32   dispTD024THEB2Set1(UINT32 panel)
{
	PanelTotalY = 262;
	PanelDimH   = 240;
	PanelVLW    = 1;
	PanelHLW    = 1;

	switch (panel)
	{
		case TD024THEB2_051:	/* 640*240, 10.36MHz */
			dispDeviceNameSet("TD024THEB2_051");
			PanelTotalX = 659;
			PanelDimW   = 480;
			PanelHBP    = 97;
			PanelVBP    = 14;
			break;

		default:
			dispDeviceNameSet("TD024THEB2 ???");
			panel = -1;
			break;
	}
	return(panel);
}

UINT32   dispTD024THEB2Set2(UINT32 panel)
{
	UINT32 sel=0, i;
	dispReg_t *pdispReg = (dispReg_t *)(0xb000a000);

	spiRST(1);
	dispSpiClkStart(0);

	dispPnlVhSet(PanelTotalX, PanelTotalY, PanelStartX, PanelStartY,\
		PanelStartX+PanelDimW-1, PanelStartY+PanelDimH-1);
	pdispReg->hsyncWidth = PanelHLW;
	pdispReg->vsyncWidth = PanelVLW;

	pdispReg->dispGpioOut[0]    =  0x00;
	pdispReg->dispGpioOutEn[0]  =  0xff;
	pdispReg->dispGpioOut[1]   |=  0x0E;	/* VD/HD/DCLK init to HI */
	pdispReg->dispGpioOutEn[1] |=  0x0E;	/* DIGTV 9~11 */
	pdispReg->dispGpioOut[2]   |=  0x01;	/* DIGTV16 */
	pdispReg->dispGpioOutEn[2] |=  0x01;
	spiRST(0);	/* 160us */
#if	SPCA==5000
	tmrUsHardWait(170);	/* delay > 160us */
#else
	#if	MINTON_YY_DEBUG==0
	hwWait(2, 500);		/* delay > 160us */
	#else
	hwWait(0, disp_reset_time_lo);		/* delay > 2ms */
	#endif
#endif
	spiRST(1);

#if	SPCA==5000
	tmrUsHardWait(2000);	/* delay > 2ms */
#else
	#if	MINTON_YY_DEBUG==0
	hwWait(0, 2);		/* delay > 2ms */
	#else	//for minton
	hwWait(0, disp_reset_time_hi);		/* delay > 10ms */
	#endif
#endif
	#if	0
	dispSpiCtrl((0x04<<10)+0x0f, 16, 0);	/* NORMAL, CP_CLK=0, PWM=0 */
	#else
	dispSpiCtrl((0x04<<10)+0x0B, 16, 0);	/* NORMAL, CP_CLK=0, PWM=0 */
	hwWait(0, 100);		/* delay > 2ms */
	#endif

	dispBgColorSet32(0x000000);
	pdispReg->pnlSel = panel & 0x0F;

	switch (panel)
	{
		case TD024THEB2_051:	/* 480*240, 10.36MHz */
	    	DISP_CLK_DIV_96_192(9);	/* 10.67 MHz */
			#if	SPCA==5000
			pdispReg->dispClkSel.dispClkDelay = 5;
			pdispReg->EvenOddRGB = 0x0A;
			#else
			pdispReg->EvenOddRGB = color[0];
			#endif
			sel = R02_INPUT_FORMAT_THROUGH	;
			break;
	}
	dispEn(1);
/*
R4
	0]: Power management
	1]: CP_CLK output on/off
	2]: PWM output on/off
	3]: Pr-charge on/off
	5:4]: Output driver capability
*/
	TD024THEB2_Vd(10);	/* DEBUG BY TedChiu - 07/07/16 14:21:12 */
	//dispSpiCtrl((0x04<<10)+1+8, 16, 0);	/* NORMAL, CP_CLK=0, PWM=0 */
	TD024THEB2_Vd(3);
	//dispSpiCtrl((0x04<<10)+3+8, 16, 0);	/* NORMAL, CP_CLK=1, PWM=0 */
	TD024THEB2_Vd(6);

	#if	SPCA==5000
	dispSpiCtrl((0x02 << 10) + sel+0x10, 16, 0);	/* Latch data edge Negative */
	#else
	dispSpiCtrl((0x02 << 10) + sel, 16, 0);	/* Latch data edge Negative */
	#endif
	for(i=0; i<sizeof(TD024THEB2_InitData)/sizeof(UINT32); i++)
		dispSpiCtrl(TD024THEB2_InitData[i], 16, 0);
	TD024THEB2_Vd(2);
	//dispSpiCtrl((0x04<<10)+3+8, 16, 0);	/* NORMAL, CP_CLK=1, PWM=1 */
	panelFlipFunc = TD024THEB2_Flip;

	pdispReg->pnlSel = panel & 0x0F;
	dispEn(1);
	dispRegUpd(2);

	return(panel);
}


#if	SPCA==5000
UINT32   disp5kTD024THEB2Set(UINT32 panel)
{
	UINT32 ret;
	ret = dispTD024THEB2Set1(panel);
	disp5kCapabSet(PanelDimW,PanelDimH,PanelStartX,PanelStartY,(panel & 0x0F)<=1);
	if(ret != -1)
		disp5kPanelInitFunSet(dispTD024THEB2Set2, panel);
	return(ret);
}

#else

UINT32   dispTD024THEB2Set(UINT32 panel)
{
	UINT32 ret;
	ret = dispTD024THEB2Set1(panel);
	ret = dispTD024THEB2Set2(panel);
	return(ret);
}
#endif

/*
R05[0]=Horizontal reverse(0),
R05[1]=Vertical reverse(0)
*/

/*
TD024THEB2_UPS052	0x00 0x00 0x04 0x20
*/
void	TD024THEB2_Flip(UINT32 mode)
{
#if	SPCA53X
	UINT32 panel = dispPnlTypeGet();
	UINT32 r5 = R5_INIT_VALUE;
	if(mode > 3)	mode = 0;
	if(mode & DISP_FLIP_H)	{	/* Left/Right Flipped */
		if((panel & 0x0F)==0x08)
			dispColorSeqChg(DISP_RGB_OUTPUT, 1);
		r5 &= ~0x0001;
	}
	else	{
		dispColorSeqChg(DISP_RGB_OUTPUT, 0);
	}
	if(mode & DISP_FLIP_V)	{	/* Up/Down Flipped */
		r5 &= ~0x0002;
	}
#if	SPCA==538
	dispColorParaUpdate();
#endif
	if(panel==TD024THEB2_051)
		dispColorSet(color[mode]);
	dispSpiCtrl((0x05<<10)+r5, 16, 0);
	dispRegUpd(2);
#endif
}

void TD024THEB2_VdSet(UINT32 hi)
{
	dispReg_t *pdispReg = (dispReg_t *)(0xb000a000);
	pdispReg->dispGpioOut[1] &= ~(1 << (9-8));		/* DIGTV09:VD */
	pdispReg->dispGpioOut[1] |=  (hi << (9-8));		/* DIGTV09:VD */
	return;
}

void TD024THEB2_Vd(UINT32 count)
{
	UINT32 i;
	for(i=0; i<count; i++)
	{
#if	0
		TD024THEB2_VdSet(0);

		#if	SPCA==5000
		tmrUsHardWait(100);	/* delay 100us */
		#else
		hwWait(1, 1);		/* delay 100us */
		#endif

		TD024THEB2_VdSet(1);

		#if	SPCA==5000
		tmrUsHardWait(16000);	/* delay 16ms */
		#else
		hwWait(0, 16);			/* delay 16ms */
		#endif
#else
		dispVsyncWait(DISP_SIGNAL_RISING);
#endif
	}
}

#if	MINTON_YY_DEBUG
void dispResetTime(UINT32 lo_t, UINT32 hi_t)
{
	disp_reset_time_lo = lo_t;
	disp_reset_time_hi = hi_t;
}
#endif

